package com.cheung.leetcode.day;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.day
 * @Author :  cheungming
 * @CreateTime : 2024-06-23 17:44:01
 * @Description : 检测大写字母
 * @Version : 1.0
 */
public class Code520Test {

    public boolean detectCapitalUse(String word) {
        // 按题意字符串要么全部大写，要么全部小写，要么首字母大写
        if (word.length() == 1) {
            return true;
        }
        // 如果首字母小写，则后续字母全部小写
        if (word.charAt(0) >= 'a' && word.charAt(0) <= 'z') {
            for (int i = 1; i < word.length(); i++) {
                if (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') {
                    return false;
                }
            }
        }

        // 如果首字母大写，则后续字母要么全部大写，要么全部小写
        if (word.charAt(0) >= 'A' && word.charAt(0) <= 'Z') {
            // 如果第二个字母小写，则后续字母全部小写
            if (word.charAt(1) >= 'a' && word.charAt(1) <= 'z') {
                for (int i = 2; i < word.length(); i++) {
                    if (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') {
                        return false;
                    }
                }
            }

            if (word.charAt(1) >= 'A' && word.charAt(1) <= 'Z') {
                // 如果第二个字母大写，则后续字母全部大写
                for (int i = 2; i < word.length(); i++) {
                    if (word.charAt(i) >= 'a' && word.charAt(i) <= 'z') {
                        return false;
                    }
                }
            }

        }

        return true;
    }

    @Test
    public void test1() {
        assert detectCapitalUse("USA");
    }

    @Test
    public void test2() {
        assert !detectCapitalUse("FlaG");
    }

    @Test
    public void test3() {
        assert detectCapitalUse("leetcode");
    }

    @Test
    public void test4() {
        assert detectCapitalUse("Google");
    }
}
